package org.xinjiang.data.migration.hainan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xinjiang.data.migration.common.utils.query.Queries;
import org.xinjiang.data.migration.hainan.convert.CrimeNameConvert;
import org.xinjiang.data.migration.hainan.dto.req.CrimeNameQueryDTO;
import org.xinjiang.data.migration.hainan.dto.res.CrimeNameResultDTO;
import org.xinjiang.data.migration.hainan.entity.CrimeNameEntity;
import org.xinjiang.data.migration.hainan.mapper.CrimeNameMapper;
import org.xinjiang.data.migration.hainan.service.ICrimeNameService;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CrimeNameServiceImpl extends ServiceImpl<CrimeNameMapper, CrimeNameEntity> implements ICrimeNameService {

    @Autowired
    private CrimeNameConvert crimeNameConvert;

    @Override
    public List<CrimeNameResultDTO> findList(CrimeNameQueryDTO dto) {
        return list(wrapper(dto)).stream().map(crimeNameConvert::convert).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<CrimeNameEntity> wrapper(CrimeNameQueryDTO query) {
        LambdaQueryWrapper<CrimeNameEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(CrimeNameEntity::getId, v));
        Queries.accept(query.getCode(), v -> wrapper.eq(CrimeNameEntity::getPCode, v));
        Queries.accept(query.getPCode(), v -> wrapper.eq(CrimeNameEntity::getPCode, v));
        Queries.accept(query.getName(), v -> wrapper.like(CrimeNameEntity::getName, v));
        return wrapper;
    }
}
